//
//  NBADBManager+Club.swift
//  NBA
//
//  Created by tiger on 2018/2/3.
//  Copyright © 2018年 Meigabay. All rights reserved.
//

import Foundation
import SQLite

//MARK: - 数据库表
internal class NBAClubTableColumn: NSObject {
    let clubId = Expression<String>("clubId")
    let name = Expression<String>("name")
    let icon = Expression<String>("icon")
    let city = Expression<String>("city")
    let venue = Expression<String>("venue")
    let introduction = Expression<String>("introduction")
    let createTime = Expression<Double>("createTime")
}

extension NBADBManager {
    internal func createClubTable() throws {
        let clubColums = NBAClubTableColumn()
        let createDownloadStoryTable = self.nbaClubTable.create(ifNotExists: true) { (table) in
            table.column(clubColums.clubId,primaryKey: true)
            table.column(clubColums.name)
            table.column(clubColums.icon)
            table.column(clubColums.city)
            table.column(clubColums.venue)
            table.column(clubColums.introduction)
            table.column(clubColums.createTime)
        }
        do {
            try self.db.run(createDownloadStoryTable)
            LogUtil.i("created club table")
        } catch {
            LogUtil.e(error)
            throw(error)
        }
    }
    
    internal func insertClub(club: NBAClub) throws {
        let columns = NBAClubTableColumn()
        let insertClub = self.nbaClubTable.insert(columns.clubId <- club.clubId,
                                                  columns.name <- club.name,
                                                  columns.venue <- club.venue,
                                                  columns.icon <- club.icon,
                                                  columns.city <- club.city,
                                                  columns.introduction <- club.introduction,
                                                  columns.createTime <- club.createTime)
        do {
            try self.db.run(insertClub)
            LogUtil.i("Insert Club Success")
        } catch {
            LogUtil.e(error)
            throw(error)
        }
    }
    
    /// 所有俱乐部
    ///
    /// - Returns: 俱乐部数组
    internal func allClubs() -> [NBAClub] {
        let colums = NBAClubTableColumn()
        let filterClub = self.nbaClubTable.filter(colums.createTime > 0)
        do {
            let results = try self.db.prepare(filterClub)
            var resultClubs = [NBAClub]()
            for result in results {
                let club = NBAClub(result: result)
                resultClubs.append(club)
            }
            return resultClubs;
        } catch {
            LogUtil.i(error)
            return [];
        }
    }
    
    /// 判断故事是否已经在数据库
    ///
    /// - Parameter story: 故事
    /// - Returns: 是否下载完成
    internal func filter(clubId: String) -> NBAClub? {
        let colums = NBAClubTableColumn()
        let filterClub = self.nbaClubTable.filter(colums.clubId == clubId)
        do {
            let results = try self.db.prepare(filterClub)
            for result in results {
                let club = NBAClub(result: result)
                return club;
            }
            return nil;
        } catch {
            LogUtil.i(error)
            return nil;
        }
    }
}
